// 新建文件 OcrResultParser.java
package com.example.testapp.utils;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class OcrResultParser {
    private static final String TAG = "OcrResultParser";
    private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static ParsedResult parse(String ocrText) {
        Log.d(TAG, "开始解析OCR结果: " + ocrText);
        ParsedResult result = new ParsedResult();

        // 1. 提取完整时间（包含日期和时间）
        parseDateTime(ocrText, result);

        // 2. 提取金额
        parseAmount(ocrText, result);

        // 3. 提取类别
        parseCategory(ocrText, result);

        // 4. 提取来源
        parseSource(ocrText, result);

        return result;
    }

    private static void parseDateTime(String text, ParsedResult result) {
        // 匹配日期时间格式：yyyy-MM-dd HH:mm:ss（允许日期和时间之间有空格）
        Pattern pattern = Pattern.compile("(\\d{4}-\\d{2}-\\d{2})\\s*(\\d{2}:\\d{2}:\\d{2})");
        Matcher matcher = pattern.matcher(text);

        if (matcher.find()) {
            String date = matcher.group(1);
            String time = matcher.group(2);
            String fullDateTime = date + " " + time;

            try {
                SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT, Locale.getDefault());
                Calendar cal = Calendar.getInstance();
                cal.setTime(sdf.parse(fullDateTime));
                result.setDateTime(cal);
                Log.d(TAG, "找到日期时间: " + fullDateTime);
            } catch (ParseException e) {
                Log.e(TAG, "日期时间解析错误: " + e.getMessage());
            }
        } else {
            Log.d(TAG, "未找到完整的日期时间格式");
        }
    }

    private static void parseAmount(String text, ParsedResult result) {
        // 匹配金额格式：支持负号、小数点和最多两位小数
        Pattern pattern = Pattern.compile("-?\\d+\\.\\d{2}");
        Matcher matcher = pattern.matcher(text);

        if (matcher.find()) {
            String amount = matcher.group();
            result.setAmount(amount);
            Log.d(TAG, "找到金额: " + amount);
        } else {
            Log.d(TAG, "未找到金额格式");
        }
    }

    private static void parseCategory(String text, ParsedResult result) {
        // 在"账单分类"关键词后提取类别
        Pattern pattern = Pattern.compile("账单分类[\\s\\S]*?\\n([^\\n>]+)");
        Matcher matcher = pattern.matcher(text);

        if (matcher.find()) {
            String category = matcher.group(1).trim();
            result.setCategory(category);
            Log.d(TAG, "找到类别: " + category);
        } else {
            Log.d(TAG, "未找到类别信息");
        }
    }

    private static void parseSource(String text, ParsedResult result) {
        // 在"付款方式"关键词后提取来源
        Pattern pattern = Pattern.compile("付款方式[\\s\\S]*?\\n([^\\n>]+)");
        Matcher matcher = pattern.matcher(text);

        if (matcher.find()) {
            String source = matcher.group(1).trim();
            result.setSource(source);
            Log.d(TAG, "找到来源: " + source);
        } else {
            Log.d(TAG, "未找到来源信息");
        }
    }

    public static class ParsedResult {
        private Calendar dateTime;
        private String amount;
        private String category;
        private String source;

        public Calendar getDateTime() {
            return dateTime;
        }

        public void setDateTime(Calendar dateTime) {
            this.dateTime = dateTime;
        }

        public String getAmount() {
            return amount;
        }

        public void setAmount(String amount) {
            this.amount = amount;
        }

        public String getCategory() {
            return category;
        }

        public void setCategory(String category) {
            this.category = category;
        }

        public String getSource() {
            return source;
        }

        public void setSource(String source) {
            this.source = source;
        }
    }
}